package goodbaby.dklt.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.bean.CourseBean;
import goodbaby.dklt.customerview.AtlasSeeActivity;
import goodbaby.dklt.fragment.StudyFragment;

/**
 * Created by Jayden on 2016/5/4 11:17.
 *
 * @version V1.0
 * @Title: CourseActivity
 * @Description: 课程/食谱
 */
public class CourseActivity extends BaseActivity {
    private List<CourseBean> data = new ArrayList();
    private int fromItem;
    private int classId = 0;
    private ArrayList<String> imgList = new ArrayList();
    private String isRC;
    private ImageView ivBack;
    private ImageView ivLeft;
    private ImageView ivRight;
    private ImageView ivSource;
    private int position = 0;
    private TextView tvTitle;
    private TextView tvWeek;


    private void getSourceImage() {
        ActionHelp.gbbQueryCourseOrRecipe(this, isRC, new ObjectCallback<List<CourseBean>>() {
                    @Override
                    public void onError(int code) {
                        if (code == 301) {
                            Intent intent = new Intent(CourseActivity.this, LoginActivity.class);
                            startActivity(intent);
                        } else {
                            ToastUtil.showToast(activity, "失败:" + code);
                        }
                    }

                    @Override
                    public void onSuccess(List<CourseBean> response) {
                        if (response != null) {
                            data = response;
                            for (CourseBean bean : data) {
                                imgList.add(bean.getImagePath());
                            }
                        }
                        init();
                    }

                    @Override
                    public Type getType() {
                        return new TypeToken<List<CourseBean>>() {
                        }.getType();
                    }
                }
        );
    }

    private void getSourceImage(int classId) {
        ActionHelp.gbbQueryCourseOrRecipe(this, isRC, classId, new ObjectCallback<List<CourseBean>>() {
                    @Override
                    public void onError(int code) {

                    }

                    @Override
                    public void onSuccess(List<CourseBean> response) {
                        if (response != null) {
                            data = response;
                            for (CourseBean bean : data) {
                                imgList.add(bean.getImagePath());
                            }
                        }
                        init();
                    }

                    @Override
                    public Type getType() {
                        return new TypeToken<List<CourseBean>>() {
                        }.getType();
                    }
                }
        );
    }

    private void init() {
        if (data.size() != 0) {
            Picasso.with(this).load(data.get(0).getImagePath()).into(ivSource);
            ivSource.setVisibility(View.VISIBLE);
        } else {
            ToastUtil.showToast(this, "暂时没有本周信息");
            ivSource.setVisibility(View.GONE);
        }

    }

    public int getLayoutResId() {
        return R.layout.activity_course;
    }

    public void initData() {
        super.initData();
        fromItem = getIntent().getIntExtra("fromItem", 2);
        if (getIntent().getExtras().containsKey("classId")) {
            classId = Integer.parseInt(getIntent().getStringExtra("classId"));
        }
    }

    public void initView() {
        super.initView();
        this.tvTitle = ((TextView) findViewById(R.id.tv_common_title));
        if (fromItem == StudyFragment.fromCourse) {
            tvTitle.setText("课程");
            isRC = "C";
        } else {
            tvTitle.setText("食谱");
            isRC = "R";
        }
        ivBack = (ImageView) findViewById(R.id.iv_common_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        ivLeft = (ImageView) findViewById(R.id.iv_week_left);
        ivLeft.setOnClickListener(this);
        ivLeft.setVisibility(View.GONE);
        ivRight = (ImageView) findViewById(R.id.iv_week_right);
        ivRight.setOnClickListener(this);
        ivRight.setVisibility(View.VISIBLE);
        tvWeek = (TextView) findViewById(R.id.tv_week);
        tvWeek.setText("本周");
        ivSource = (ImageView) findViewById(R.id.iv_source);
        ivSource.setOnClickListener(this);
        if (classId != 0) {
            getSourceImage(classId);
        } else {
            getSourceImage();
        }
    }

    public void viewClick(View paramView) {
        super.viewClick(paramView);
        switch (paramView.getId()) {
            default:
                break;
            case R.id.iv_common_back:
                finish();
                break;
            case R.id.iv_week_left:
                ivLeft.setVisibility(View.GONE);
                ivRight.setVisibility(View.VISIBLE);
                tvWeek.setText("本周");
                if (data.size() > 0) {
                    Picasso.with(this).load(data.get(0).getImagePath()).into(ivSource);
                    this.ivSource.setVisibility(View.VISIBLE);
                    position = 0;
                } else {
                    ToastUtil.showToast(this, "暂时没有本周信息");
                    ivSource.setVisibility(View.GONE);
                }
                break;
            case R.id.iv_week_right:
                ivLeft.setVisibility(View.VISIBLE);
                ivRight.setVisibility(View.GONE);
                tvWeek.setText("下周");
                if (data.size() > 1) {
                    Picasso.with(this).load(data.get(1).getImagePath()).into(ivSource);
                    ivSource.setVisibility(View.VISIBLE);
                    position = 1;
                } else {
                    ToastUtil.showToast(this, "暂时没有下周信息");
                    this.ivSource.setVisibility(View.GONE);
                }
                break;
            case R.id.iv_source:
                Intent localIntent = new Intent(this, AtlasSeeActivity.class);
                localIntent.putExtra("fromType", 5);
                localIntent.putExtra("Urls", imgList);
                localIntent.putExtra("position", position);
                startActivity(localIntent);
                break;
        }

    }
}
